﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using CommonControlLibrary.Common;
using QueryResource.Common;
using QueryResource.ViewModel;
using DBAccessLibrary.Model;

namespace DevControlLibrary.UI.Comparsion
{
    /// <summary>
    /// DataComparsionWindow.xaml 的交互逻辑
    /// </summary>
    [System.ComponentModel.DesignTimeVisible(false)]
    public partial class DataComparsionControl : DockPanel
    {
        DataComparsionViewModel _viewModel;
        public DataComparsionControl()
        {
            InitializeComponent();
           
            _viewModel = new DataComparsionViewModel
                             {
                                 ViewInvoke = action => this.InvokeEx(action)
                             };

            this.DataContext = ViewModel;
        }

        public DataComparsionViewModel ViewModel 
        { 
            get { return _viewModel; }
            private set { _viewModel = value; }
        }
        private void gridview_FocusedRowChanged(object sender, DevExpress.Xpf.Grid.FocusedRowChangedEventArgs e)
        {
            TableComparisonEntity tc = e.NewRow as TableComparisonEntity;
            if (tc == null) 
                return;
            this.sourceGrid.ItemsSource = _viewModel.ColumnComparisonList.Where(o => o.TableName.ToUpper() == tc.TableName.ToUpper() && o.SourceType == "Source").ToList();
            this.targetGrid.ItemsSource = _viewModel.ColumnComparisonList.Where(o => o.TableName.ToUpper() == tc.TableName.ToUpper() && o.SourceType == "Target").ToList();
        }
        private void TreeView_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
        {
            TreeView tv = sender as TreeView;
            if (tv != null) 
                tv.Focus();
        }

        ScrollViewer sv1, sv2;
        private void gridview2_ScrollChanged(object sender, ScrollChangedEventArgs e)
        {
            sv2 = (e.OriginalSource as ScrollViewer);
            if (sv1 == null) return;
            sv1.ScrollToHorizontalOffset(e.HorizontalOffset);
            sv1.ScrollToVerticalOffset(e.VerticalOffset);
        }

        private void gridview1_ScrollChanged(object sender, ScrollChangedEventArgs e)
        {
            sv1 = (e.OriginalSource as ScrollViewer);

            if (sv2 != null)
            {
                sv2.ScrollToHorizontalOffset(e.HorizontalOffset);
                sv2.ScrollToVerticalOffset(e.VerticalOffset);
            }
        }

        public void Refresh(Action action = null)
        {
            this.tv1panel.Caption = "目标数据";
            this.tv2panel.Caption = "源数据";
            this.masterpanel.Caption = "结果";
            this.masterGrid.ItemsSource = null;
            this.sourceGrid.ItemsSource = null;
            this.targetGrid.ItemsSource = null;
            _viewModel.Refresh(action);
        }


        public void Connection_Click()
        {
            DataComparsionConnection c = new DataComparsionConnection();
            if (c.ShowDialog() == true)
            {
                _viewModel.Connect(c.DataSource, c.TargetSource, () => {
                    this.InvokeEx(() =>
                                      {
                                          this.tv1panel.Caption = "目标数据(" + tv1.Items.Count + ")";
                                          this.tv2panel.Caption = "源数据(" + tv2.Items.Count + ")";
                                      });
                });
            }
        }

        public  void Comparsion_Click()
        {
            _viewModel.Comparsion(list => {
                this.InvokeEx(() =>
                                  {
                                      this.masterGrid.ItemsSource = list;
                                      this.masterpanel.Caption = "结果(表结构不同->" + list.Count + ")";
                                  });
            });
           
        }

        public void ButtonExport_Click()
        {
            bool isDeleteAll = MessageBoxHelper.ShowMessage("是否删除表数据，再导入?", "提示", MessageBoxButton.OKCancel, MessageBoxImage.Question) == MessageBoxResult.OK;
            _viewModel.ExportData(isDeleteAll);
        }
        public void ButtonPartExport_Click()
        {
            if (this.tv1.SelectedItem == null)
            {
                MessageBoxHelper.Show("请在目标数据中选择一个节点。");
                return;
            }
            bool isDeleteAll = MessageBoxHelper.ShowMessage("是否删除表数据，再导入?", "提示", MessageBoxButton.OKCancel, MessageBoxImage.Question) == MessageBoxResult.OK;

            _viewModel.PartExport(tv1.SelectedItem as DBComparsionEntity, isDeleteAll);


        }
        public void ButtonRowContent_Click()
        {
            if (this.tv1.SelectedItem == null)
            {
                MessageBoxHelper.Show("请在目标数据中选择一个节点。");
                return;
            }
            DBComparsionEntity entity =tv1.SelectedItem as DBComparsionEntity;
            if (entity == null)
                return;
            if(ViewModel.TargetSourceList.FirstOrDefault(o => o.Name == entity.Name) == null)
            {
                MessageBoxHelper.Show("源数据中没有该表，无法进行对比.");
                return;
            }
            List<FieldEntity> list = ViewModel.GetFieldList(entity.Name);
            if(list == null)
            {
                MessageBoxHelper.Show("该表不存在列信息");
                return;
            }

            DataFilterColumnWindow window = new DataFilterColumnWindow(entity.Name);
            window.DataBind(list);
            if (window.ShowDialog() != true) 
                return;
            DataRowDetailedWindow win = new DataRowDetailedWindow(
                entity.Name,
                window.DataFilterRight.ToArray(),
                window.DataFilterLeft.ToArray(),
                ViewModel);
            win.ShowDialog();
        }


        private void MasterMenuSelect_Click(object sender, RoutedEventArgs e)
        {
            if (gridview.SelectedRows.Count <= 0) 
                return;
            foreach (TableComparisonEntity row in gridview.SelectedRows)
            {
                row.IsUpdate = !row.IsUpdate;
            }
            masterGrid.RefreshData();
        }
    }


}
